#include "oled.h"

#define OLED_I2C_ADDR 0x78
extern I2C_HandleTypeDef hi2c1;


//张
const uint8_t Hanzi_Zhang[] = 
	{
 0x00, 0xf2, 0x12, 0x12, 0x12, 0x1e, 0x00, 0x00, 0xfe, 0x40, 0x60, 0x30,
    0x18, 0x0c, 0x06, 0x00, 0x00, 0x41, 0x61, 0x39, 0x0d, 0x07, 0x00, 0x01,
    0xff, 0x83, 0x07, 0x0d, 0x19, 0x31, 0x21, 0x00

         };

	
//春
const uint8_t Hanzi_Chun[] = 
	{
  0x00, 0x00, 0x00, 0x00, 0x24, 0xe4, 0x74, 0x3e, 0x24, 0x24, 0x24, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x09, 0x0d, 0x07, 0x7d, 0x55, 0x55,
    0x55, 0x7d, 0x07, 0x0d, 0x09, 0x01, 0x00, 0x00
};
	
//天
const uint8_t Hanzi_Tian[] = 
	{
 0x00, 0x00, 0x40, 0x40, 0x44, 0x44, 0xc4, 0xfc, 0x44, 0x44, 0x44, 0x44,
    0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x20, 0x30, 0x1c, 0x07, 0x01, 0x00,
    0x01, 0x07, 0x0c, 0x18, 0x30, 0x20, 0x00, 0x00
         };


	
//写指令	
void OLED_WriteCmd(uint8_t cmd) 
{
 uint8_t buf[2] = {0x00, cmd}; // 0x00表示写指令
    HAL_I2C_Master_Transmit(&hi2c1, OLED_I2C_ADDR, buf, 2, 100);
}


//写数据
void OLED_WriteData(uint8_t data) 
{
     uint8_t buf[2] = {0x40, data}; // 0x40表示写数据
    HAL_I2C_Master_Transmit(&hi2c1, OLED_I2C_ADDR, buf, 2, 100);
}


//初始化
void OLED_Init(void) 
{ 
  
	//OLED寄存器配置
  OLED_WriteCmd(0xAE); //关闭显示
  OLED_WriteCmd(0x00); //列地址低位
  OLED_WriteCmd(0x10);//列地址高位
  OLED_WriteCmd(0x40);//显示起始行
  OLED_WriteCmd(0xB0);//页地址
  OLED_WriteCmd(0x81); //对比度
  OLED_WriteCmd(0xCF);//0.96寸最佳对比度
  OLED_WriteCmd(0xA6); //正常显示
  OLED_WriteCmd(0xA8); //多路复用率
  OLED_WriteCmd(0x3F);//64行
  OLED_WriteCmd(0xD3);//显示偏移
  OLED_WriteCmd(0x00);
  OLED_WriteCmd(0xD5);//时钟分频
  OLED_WriteCmd(0x80);
  OLED_WriteCmd(0xD9); //预充电周期
  OLED_WriteCmd(0xF1);//0.96寸兼容值
  OLED_WriteCmd(0xDA); //COM引脚配置
  OLED_WriteCmd(0x12);
  OLED_WriteCmd(0xDB);//VCOMH电压 
  OLED_WriteCmd(0x30);
  OLED_WriteCmd(0x8D);//充电垒设置
    OLED_WriteCmd(0x14);//开启充电垒
  OLED_WriteCmd(0xAF);//开启显示
  OLED_Clear();
}


void OLED_Clear(void)
{
    uint8_t i, j;
    for(i=0; i<8; i++)
    {
        OLED_WriteCmd(0xB0 + i);
        OLED_WriteCmd(0x00);
        OLED_WriteCmd(0x10);
        for(j=0; j<128; j++)
        {
            OLED_WriteData(0x00);
        }
    }
}



void OLED_SetPos(uint8_t x, uint8_t y) 
{
   OLED_WriteCmd(0xB0 + y);
    OLED_WriteCmd((x & 0x0F) | 0x00);
    OLED_WriteCmd(((x & 0xF0) >> 4) | 0x10);
}






void OLED_ShowHanzi(uint8_t x, uint8_t y, const uint8_t *hanzi)
{
 

    OLED_SetPos(x, y);
    for (uint8_t i = 0; i < 16; i++)
    {
        OLED_WriteData(hanzi[i]);
    }
    OLED_SetPos(x, y + 1);
    for (uint8_t i = 16; i < 32; i++)
    {
        OLED_WriteData(hanzi[i]);
    }
}